package com.doradance.account.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.AccountManager;
import com.doradance.account.utils.WechatHelper;
import com.doradance.app.MainActivity;
import com.doradance.app.appactivity.AddBabyActivity;
import com.doradance.app.appactivity.SelectBabyActivity;
import com.doradance.common.http.TokenInfo;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.utils.SLog;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.doradance.account.AccountManager.getTokenInfo;
import static com.doradance.account.ui.LoginPhoneActivity.LOGINADD;


/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/1/9
 */

public class LoginActivity extends BaseActivity {
    private static boolean isExit = false;
    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_login, container, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        auautoLogin();
        }

    @Override
    protected void onResume() {
        super.onResume();
        getCustomToolbar().setTitle("登录");
        regToWx();
        hideToolbar();
    }



    private void regToWx() {
        WechatHelper.getInstance().init(this);
    }

    //自动登录
    private void auautoLogin() {
        TokenInfo tokenInfo = getTokenInfo();
        if (TextUtils.isEmpty(tokenInfo.getAccessToken())) {//未登录
        }
        else if(!TextUtils.isEmpty(tokenInfo.getAccessToken()) && SharePreferenceUtil.getString(getApplicationContext(),"isOptimize","isOptimize").equals("false")){
            AddBabyActivity.start(this,LOGINADD);
        }else {//自动登录
            SelectBabyActivity.start(LoginActivity.this);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(this, "在按一次退出程序", Toast.LENGTH_SHORT).show();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
            } else {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
                System.exit(0);//退出程序
//                           finish();
            }
        }
        return false;
    }

    @OnClick({R.id.btn_login, R.id.ll_agreement,R.id.btn_phone_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                WechatHelper.getInstance().sendAuthWechat();
                        break;
            case R.id.ll_agreement:
                AgreementActivity.start(LoginActivity.this);
                break;
            case R.id.btn_phone_login:
                LoginPhoneActivity.start(LoginActivity.this);
                break;
        }
    }
}
